package com.ruoyi.controller;

import com.aliyun.oss.OSSClient;
import com.ruoyi.domain.Result;
import com.ruoyi.domain.UserPeople;
import com.ruoyi.service.UserPeopleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/login")
public class UserController {

    @Resource
    private UserPeopleService userPeopleService;

    @Value("${oss.bucket}")
    String bucket;

    @Value("${oss.hostName}")
    String hostName;

    @Resource
    private OSSClient ossClient;

    //账号密码登录
    @RequestMapping("/userlogin")
    public Result userLogin(@RequestBody UserPeople userPeople) throws UnsupportedEncodingException {
       return Result.succeed(userPeopleService.userLogin(userPeople));
    }

    //手机号验证码登录
    @RequestMapping("/phoneLogin")
    public Result phoneLogin(@RequestBody UserPeople userPeople){
        return Result.succeed(userPeopleService.phoneLogin(userPeople));
    }

    //发送短信获取验证码
    @RequestMapping("/sendcode")
    public Result sendcode(@RequestParam("peoplePhone") String peoplePhone){
        return Result.succeed(userPeopleService.sendcode(peoplePhone));
    }

    //列表
    @RequestMapping("/list")
    public Result userlist(){
        return Result.succeed(userPeopleService.list());
    }

    //删除
    @RequestMapping("/del")
    public Result userdel(@RequestParam("peopleId") Integer peopleId){
        return Result.succeed(userPeopleService.removeById(peopleId));
    }

    //添加修改
    @RequestMapping("/save")
    public Result save(@RequestBody UserPeople userPeople){
        return Result.succeed(userPeopleService.save(userPeople));
    }

    //oss视频上传
    @RequestMapping("/videoupload")
    public Result VideoUpload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        ossClient.putObject(bucket, filename, file.getInputStream());
        return Result.succeed(hostName+filename);
    }


    //测试数据
    @GetMapping("/hello")
    public String hello() {
        return "欢迎访问 hangge.com";
    }



}
